package com.fzy.modules.goods.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fzy.core.common.IDUtils;
import com.fzy.core.common.PageObject;
import com.fzy.modules.goods.dao.GoodsDao;
import com.fzy.modules.goods.dao.GoodsPicDao;
import com.fzy.modules.goods.entity.Goods;
import com.fzy.modules.goods.entity.GoodsMore;
import com.fzy.modules.goods.entity.GoodsPic;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 商品操作
 */
@Service
public class GoodsService {
    @Autowired
    GoodsDao goodsDao;
    @Autowired
    private GoodsPicDao goodsPicDao;
    /**
     *查询商品信息
     */
    public PageObject<GoodsMore> findGoods(GoodsMore goodsMore,int page,int rows){
    	//开启分页，注意此行代码一定要紧挨在查询语句之前
    	PageHelper.startPage(page, rows);
    	List<GoodsMore> goodss = goodsDao.findGoodsMore(goodsMore);
    	//查询之后获取分页对象
    	PageInfo<GoodsMore> pageInfo=new PageInfo<GoodsMore>(goodss);
    	//构建我们自定义的分页实体返回类
    	PageObject<GoodsMore> pageObject=new PageObject<GoodsMore>();
    	pageObject.setTotal(pageInfo.getTotal());
    	pageObject.setRows(goodss);
        return pageObject;
    }
    /**
     * 根据条件查询商品信息
     */
    public List<Goods> isOne(Goods goods){
       return goodsDao.isOne(goods);
    }
    /**
     * 查询某个分类下是否有产品
     */
    public Integer isWithGoodsUnderCategory(String categoryId ){
        return goodsDao.isWithGoodsUnderCategory(categoryId);
    }
    /**
     * 查询某个品牌下是否有产品
     */
    public Integer isWithGoodsUnderBrand(String brandId){
        return goodsDao.isWithGoodsUnderBrand(brandId);
    }
    /**
     * 添加产品
     */
    public Integer save(Goods goods){
        //String uuid= UUID.randomUUID().toString().toUpperCase();
        goods.setId(IDUtils.genItemId());
        if(StringUtils.isEmpty(goods.getCompanyId())){
            goods.setCompanyId("4D74E1E4-A129-11E4-9B6A-782BCBD7746B");
        }
        if(StringUtils.isEmpty(goods.getDataOrg())){
            goods.setDataOrg("01010001");
        }
        return goodsDao.save(goods);
    }
    /**
     * 修改产品信息
     */
    public Integer update(Goods goods){return goodsDao.update(goods);}
    /**
     * 删除产品
     */
    public Integer delete(String id){return goodsDao.delete(id);}

    /**
     * 查询商品信息
     */
    public List<Goods> goodList(Goods goods){
        return goodsDao.findGoods(goods);
    }

    
	public List<Goods> goodsList(Goods goods) {
        List<Goods> goodss = goodsDao.findGoods(goods);
        return  goodss;
	}
	public List<GoodsMore> findGoodsMore(GoodsMore goodsMore) {
		
		return goodsDao.findGoodsMore(goodsMore);
	}
	public Integer saveGoods(Goods goods, String picAddr) throws Exception{
		goods.setId(IDUtils.genItemId());
		if(StringUtils.isEmpty(goods.getSpecPy())){
			goods.setSpecPy(goods.getSpec());
		}
        if(StringUtils.isEmpty(goods.getCompanyId())){
            goods.setCompanyId("4D74E1E4-A129-11E4-9B6A-782BCBD7746B");
        }
        if(StringUtils.isEmpty(goods.getDataOrg())){
            goods.setDataOrg("01010001");
        }
        goodsDao.save(goods);
        String[] picStrs=picAddr.split(",");
        GoodsPic goodsPic=new GoodsPic();
		goodsPic.setGoodsId(goods.getId());
		goodsPic.setCreateDate(IDUtils.getMysqlCurDate());
        for(String picStr:picStrs){
        	if(picStr!=null&&picStr!=""){
        		goodsPic.setPicAddr(picStr);
        		goodsPic.setId(IDUtils.genItemId());
        		goodsPicDao.save(goodsPic);
        	}
        }
        return 200;
	}
	public List<GoodsMore> listBySoBillIds(String goodsIds) {
		return goodsDao.listBySoBillIds(goodsIds);
	}

	

}
